﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchEdu.Framework.Logging;
using SchEdu.Framework.Validators;
using SchEdu.Framework.DataModel;
using System.Windows.Forms.DataVisualization.Charting;

namespace SchEdu.Framework.Win
{
    public partial class ComparisonChart : UserControl
    {
        private Logger logger = new Logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString());

        protected Logger Logger
        {
            get { return logger; }
            set { logger = value; }
        }

        public ComparisonChart()
        {
            InitializeComponent();
        }

        private string CombineKey(SchEduDataSet.PerformanceRow performance)
        {
            return string.Format("{0}-{1}", performance.ObjectiveGuid.ToString(), performance.MachineGuid.ToString());
        }


        public void DisplayChart(SchEduData schEduData, SolutionList solutions)
        {
            tabControl.TabPages.Clear();
            solutions.SortByID();
            Dictionary<string, string> performanceKeys = new Dictionary<string, string>();

            foreach (SchEduDataSet.SolutionRow solution in solutions)
            {
                foreach (SchEduDataSet.PerformanceRow performance in solution.GetPerformanceRows())
                {

                    if (performanceKeys.ContainsKey(CombineKey(performance)) == true)
                    {
                        continue;
                    }
                    performanceKeys.Add(CombineKey(performance), performance.Title);

                }
            }


            foreach (KeyValuePair<string, string> performanceKey in performanceKeys)
            {
                TabPage tp = new TabPage(performanceKey.Value);
                tabControl.TabPages.Add(tp);
                Chart chart = new Chart();
                tp.Controls.Add(chart);
                chart.Dock = DockStyle.Fill;
                ChartArea chartArea = new ChartArea(performanceKey.Value);
                chart.ChartAreas.Add(chartArea);

                int i = 1;
                foreach (SchEduDataSet.SolutionRow solution in solutions)
                {
                    foreach (SchEduDataSet.PerformanceRow performance in solution.GetPerformanceRows())
                    {

                        if (CombineKey(performance) != performanceKey.Key)
                        {
                            continue;
                        }
                        string serieName = string.Format("{0}-{1}", i++, solution.Title);
                        Series series = new Series(serieName);
                        series.ChartType = SeriesChartType.Column;

                        chart.Series.Add(series);
                        series.Points.AddXY(performance.Title, performance.Value);
                        Legend legend = new Legend(serieName);
                        chart.Legends.Add(legend);
                    }
                }

            }

        }

    }
}
